﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Adams.Admin.DataAccess.Console
{
    public static class Password
    {
        public static Adams.Admin.DataTransfer.Console.Password.PageLoad PageLoad(int userId)
        {
            var data = new Adams.Admin.DataTransfer.Console.Password.PageLoad();
            data.User = new Adams.Shared.Entities.Console.User();

            data.User.ID = userId;

            return data;
        }

        public static void SavePassword(int userId, string passwordHash)
        {
            DB.Run(@"
UPDATE Console.Users SET
    PasswordHash = @PasswordHash
WHERE UserID = @UserID
    ", CommandType.Text,
                    DB.CreateParameter("@UserID", userId),
                    DB.CreateParameter("@PasswordHash", passwordHash));
        }

        public static Adams.Admin.DataTransfer.Console.Password.UserInfo GetUserInfo(int userId)
        {
            var data = new Adams.Admin.DataTransfer.Console.Password.UserInfo();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT *
FROM Console.Users
WHERE UserID = @UserID
", CommandType.Text,
                DB.CreateParameter("@UserID", userId)))
            {
                dr.Read();
                data.UserID = dr["UserID"].DbInt();
                data.PasswordHash = dr["PasswordHash"].DbString();
                data.PasswordSalt = dr["PasswordSalt"].DbString();
            }

            return data;
        }
    }
}
